package com.itheima.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.dto.OrderDeliveryDto;
import com.itheima.mall.dto.OrderDto;
import com.itheima.mall.dto.OrderMoneyInfo;
import com.itheima.mall.dto.OrderReceiverInfo;
import com.itheima.mall.mapper.OmsOrderItemMapper;
import com.itheima.mall.mapper.OmsOrderMapper;
import com.itheima.mall.mapper.OrderOperateHistoryMapper;
import com.itheima.mall.pojo.Order;
import com.itheima.mall.pojo.OrderItem;
import com.itheima.mall.pojo.OrderOperateHistory;
import com.itheima.mall.service.OmsOrderService;
import com.itheima.mall.vo.OrderVo;
import com.itheima.mall.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OmsOrderServiceImpl implements OmsOrderService {
   @Resource
   private OmsOrderMapper omsOrderMapper;
   @Resource
   private OrderOperateHistoryMapper orderOperateHistoryMapper;
   @Resource
   private OmsOrderItemMapper omsOrderItemMapper;

    /**
     * 批量删除订单
     * @param ids
     * @return
     */
    public int deleteByIds(List<Long> ids) {
        int i = omsOrderMapper.deleteBatchIds(ids);
        return i;
    }

    /**
     * 根据条件查询订单
     * @param orderDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResult list(OrderDto orderDto, Integer pageNum, Integer pageSize) {
        IPage page=new Page(pageNum,pageSize);
        LambdaQueryWrapper<Order> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(orderDto.getCreateTime()),Order::getCreateTime,orderDto.getCreateTime());
        queryWrapper.eq(StringUtils.isNotEmpty(orderDto.getOrderSn()),Order::getOrderSn,orderDto.getOrderSn());
        if (StringUtils.isNotEmpty(orderDto.getReceiverKeyword())){
            queryWrapper.like(Order::getReceiverName,orderDto.getReceiverKeyword());
            queryWrapper.or();
            queryWrapper.like(Order::getReceiverPhone,orderDto.getReceiverKeyword());
        }
        queryWrapper.eq(orderDto.getStatus()!=null,Order::getStatus,orderDto.getStatus());
        queryWrapper.eq(orderDto.getOrderType()!=null,Order::getOrderType,orderDto.getOrderType());
        queryWrapper.eq(orderDto.getSourceType()!=null,Order::getSourceType,orderDto.getSourceType());
        IPage page1 = omsOrderMapper.selectPage(page, queryWrapper);
        PageResult pageResult = new PageResult(pageNum, pageSize, (int) page1.getTotal(), page1.getRecords());
        return pageResult;
    }

    /**
     * 批量关闭订单
     * @param ids
     * @param note
     * @return
     */
    @Override
    public int close(List<Long> ids, String note) {
        LambdaQueryWrapper<Order> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.in(Order::getId,ids);
        Order order = new Order();
        order.setStatus(4);
        int count = omsOrderMapper.update(order, queryWrapper);
        for (Long id : ids) {
            OrderOperateHistory history = new OrderOperateHistory();
            history.setOrderId(id);
            history.setOperateMan("后台管理员");
            history.setCreateTime(new Date());
            history.setOrderStatus(4);
            history.setNote("订单关闭:" + note);
orderOperateHistoryMapper.insert(history);
        }
        return count;
    }
    /**
     * 批量发货
     * @param deliveryParamList
     * @return
     */
    @Override
    public void delivery(List<OrderDeliveryDto> deliveryParamList) {
        for (OrderDeliveryDto orderDeliveryDto : deliveryParamList) {
            Order order = new Order();
            order.setId(orderDeliveryDto.getOrderId());
            order.setDeliverySn(orderDeliveryDto.getDeliverySn());
            order.setDeliveryCompany(orderDeliveryDto.getDeliveryCompany());
            order.setStatus(2);
           omsOrderMapper.updateById(order);
            OrderOperateHistory history = new OrderOperateHistory();
            history.setOrderId(orderDeliveryDto.getOrderId());
            history.setOperateMan("后台管理员");
            history.setCreateTime(new Date());
            history.setOrderStatus(2);
            history.setNote("完成发货");
            orderOperateHistoryMapper.insert(history);
        }

    }
    /**
     * 修改订单费用信息
     * @param moneyInfoParam
     * @return
     */
    @Override
    public int moneyInfo(OrderMoneyInfo moneyInfoParam) {
        Order order = new Order();
        order.setId(moneyInfoParam.getOrderId());
        order.setFreightAmount(moneyInfoParam.getFreightAmount());
        order.setDiscountAmount(moneyInfoParam.getDiscountAmount());
        order.setStatus(moneyInfoParam.getStatus());
        order.setModifyTime(new Date());
        int i = omsOrderMapper.updateById(order);
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(moneyInfoParam.getOrderId());
        history.setOperateMan("后台管理员");
        history.setCreateTime(new Date());
        history.setOrderStatus(moneyInfoParam.getStatus());
        history.setNote("修改费用信息");
        orderOperateHistoryMapper.insert(history);
        return i;
    }
    /**
     * 备注订单
     * @param order
     * @return
     */
    @Override
    public int note(Order order) {
        order.setModifyTime(new Date());
        int i = omsOrderMapper.updateById(order);
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(order.getId());
        history.setOperateMan("后台管理员");
        history.setCreateTime(new Date());
        history.setOrderStatus(order.getStatus());
        history.setNote("修改备注信息:"+order.getNote());
        orderOperateHistoryMapper.insert(history);
        return i;
    }
    /**
     * 修改收货人信息
     * @param receiverInfoParam
     * @return
     */
    @Override
    public int receiverInfo(OrderReceiverInfo receiverInfoParam) {
        Order order = new Order();
        BeanUtils.copyProperties(receiverInfoParam,order);
        order.setId(receiverInfoParam.getOrderId());
        order.setModifyTime(new Date());
        int i = omsOrderMapper.updateById(order);
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(order.getId());
        history.setOperateMan("后台管理员");
        history.setCreateTime(new Date());
        history.setOrderStatus(order.getStatus());
        history.setNote("修改收货人信息");
        orderOperateHistoryMapper.insert(history);
        return i;
    }
    /**
     * 获取订单详情：订单信息、商品信息、操作记录
     * @param id
     * @return
     */
    @Override
    public OrderVo details(Long id) {
        Order order = omsOrderMapper.selectById(id);
        LambdaQueryWrapper<OrderOperateHistory> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderOperateHistory::getOrderId,id);
        List<OrderOperateHistory> orderOperateHistories = orderOperateHistoryMapper.selectList(queryWrapper);
        LambdaQueryWrapper<OrderItem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderItem::getOrderId,id);
        List<OrderItem> orderItems = omsOrderItemMapper.selectList(lambdaQueryWrapper);
        OrderVo vo = new OrderVo();
        BeanUtils.copyProperties(order,vo);
        vo.setOrderItemList(orderItems);
        vo.setHistoryList(orderOperateHistories);
        return vo;
    }
}
